﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLBH.DataModel;
using QLBH.Base;
using QLBH.Model.Controller;
using System.Data;
using System.Reflection;
using System.Data.SqlTypes;
using QLBH.ModelInterface;

namespace QLBH.FeatureModel
{
    public class PhieuMuaHangModel:IPhieuMuaHangModel
    {
        public Guid MaPhieu { set; get; }
        public String SoPhieu {set;get;}
        public Guid MaKho { set; get; }
        public String TenKho { set; get; }
        public Guid UserID { set; get; }
        public String UserName { set; get; }
        public DateTime NgayTao { set; get; }
        public DateTime NgayGui { set; get; }
        public decimal TongTien { set; get; }        

        public PhieuMuaHangModel()
        {            
        }

        public void InitFromEntity(PHIEUMUAHANG entity)
        {
            MaPhieu = entity.MUAHANGID;
            SoPhieu = entity.SOPHIEU;
            MaKho = entity.KHOID;

            KHO kho = KhoController.GetInstance().FindBy(kh => kh.KHOID == entity.KHOID).FirstOrDefault();

            if (kho!=null)
            {
                TenKho = kho.TENKHO;
            }

            UserID = AccountHandler.GetCurrentUserLogin().NVID;
            UserName = AccountHandler.GetCurrentUserLogin().TENNV;

            NgayTao = (DateTime)entity.NGAYTAO;
            NgayGui = (DateTime)entity.NGAYGUI;

            TongTien = (decimal)entity.TONGTIEN;
        }

        public PHIEUMUAHANG ToEntity()
        {
            PHIEUMUAHANG entity = new PHIEUMUAHANG();
            entity.KHOID = MaKho;
            entity.MUAHANGID = MaPhieu;
            entity.NGAYGUI = NgayGui == DateTime.MinValue ? SqlDateTime.MinValue.Value : NgayGui;
            entity.NGAYTAO = NgayTao;
            entity.NGUOITAOID = UserID;
            entity.SOPHIEU = SoPhieu;
            entity.TONGTIEN = TongTien;

            return entity;
        }
    }
}
